package com.kcnet.qk.controller.group;

import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.app.AppContext;
import com.kcnet.qk.constant.Const;
import com.kcnet.qk.constant.StringConst;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.controller.dynamic.TemeLineSendHomeCTR;
import com.kcnet.qk.util.FBStringUtil;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;
import com.kcnet.qk.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jesse.nativelogger.NLogger;

import static com.kcnet.qk.constant.Const.LIST_VIEW;

/**
 * Created by Administrator on 2018/5/10 0010.
 * <p>
 * 邀请好友点赞主页
 */

public class TeamFriendZanHomeCTR extends BasePeopleController {

    private static TeamFriendZanHomeCTR releaseimgctr;
    private static String TAG = "邀请好友点赞主页";
    private boolean isClickSeatch;
    private boolean isSearchHomeList; //是否进入了搜索主页列表
    private boolean isJoinHomeList; //是否进入了主页列表
    private boolean isJoinHomeDetail = false; //是否进入 主页详情
    private boolean isJoinFriendsList; //是否进入选择好友列表
    private boolean isFrinedsListBack;

    private int currentFriendsCount;
    private int maxFriendsCount = 5;

    private String content ="小米";
    protected Map<String, Boolean> mRecordMap = Collections.synchronizedMap(new HashMap());

    public static TeamFriendZanHomeCTR getInstance() {
        if (releaseimgctr == null) {
            synchronized (TeamAttsTeamCTR.class) {
                if (releaseimgctr == null) {
                    releaseimgctr = new TeamFriendZanHomeCTR();
                }
            }
        }
        return releaseimgctr;
    }


    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();

        mType = event.getEventType();


        if (!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBLauncherUI)) {
            NLogger.d(TAG, "1、点击搜索框");
//            Onclic_shell("81", "38");
            RootShell.click(81,38);
            isClickSeatch = true;
            initFbMain = true;
        }  else if (isClickSeatch && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")) {
            Log.d(TAG, "2、事件 点击搜索框-输入主页名称");
            sleepTimedDefault();
            //  Onclic_shell("81", "38");
            inputSayHiSentence(content);

            NLogger.d(TAG, "3、输入完成事件");
            RootShell.keyevent(66);
            isSearchHomeList = true;
            isClickSeatch = false;
        } else if (isSearchHomeList && (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals("android.support.v7.widget.RecyclerView")
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.support.v7.widget.RecyclerView"))){
            NLogger.d(TAG, "4、搜索结果页");
            if( RootShell.click(378,110)) {
                isSearchHomeList = false;
                isJoinHomeList = true;//取消
            }
        }
        else if (isJoinHomeList && (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals("android.support.v7.widget.RecyclerView")
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.support.v7.widget.RecyclerView"))) {
            NLogger.d(TAG, "5、事件 到达搜索主页");
            begin();

        } else if (isJoinHomeDetail && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")) {
            NLogger.d(TAG, "6、进入了主页详情");
            sleepTimedDefault();
            tagTextClick(FBStringUtil.getString(StringConst.TIMELINE_FENXIANG));
        } else if (isJoinHomeDetail && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.4N5")) {
            NLogger.d(TAG, "7、点击了分享按钮");
            sleepTimedDefault();
            tagTextClick(FBStringUtil.getString(StringConst.TEAM_YQHYZZY));
        } else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.widget.friendselector.CaspianFriendSelectorActivity")) {
            NLogger.d(TAG, "8、进入好友列表");
            isJoinHomeDetail = false;
            isJoinFriendsList = true;
        } else if (isJoinFriendsList && mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.widget.ListView")) {
            //好友列表加载出了数据 开始执行点击好友数量
            beginSelectFriend();
        } else if (isFrinedsListBack && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")) {
            NLogger.d(TAG, "9、回到了主页详情 点击返回");
            if (performBackClick()) {
                isJoinHomeList = true;
                isFrinedsListBack = false;
            }
            //点击返回
        }else if(isJoinHomeList && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")){
            NLogger.d(TAG, "10、继续执行点击下一个主页");
            begin();
        }

    }

    private void begin() {

        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, "android.support.v7.widget.RecyclerView");
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                    //  ToastUtil.showToast(AppContext.getInstance().getContext(),"scrllorNodeInfo listView null");
                    return;
                }

                for (int q = 0; q < scrllorNodeInfo.getChildCount(); q++) {

                    if (currentAddCount == maxAddCount) {
                        currentAddCount = 0;
                        mRecordMap.clear();
                        stopTask();
                        break;
                    }

                    AccessibilityNodeInfo nodeInfo = scrllorNodeInfo.getChild(q);
                    if(nodeInfo.getChildCount()==0){
                        continue;
                    }
                    String homeDesc = "";
                    try {
                        homeDesc = nodeInfo.getChild(0).getChild(0).getChild(0).getContentDescription().toString();
                        //如果里面包含赞主页的这个字 就取第一种方式
                        if(homeDesc.contains("赞主页")){
                            homeDesc = nodeInfo.getChild(0).getChild(0).getContentDescription().toString();
                        }
                    }catch (Exception e){
                        //e.printStackTrace();
                    }
                    if(TextUtils.isEmpty(homeDesc)){
                        continue;
                    }
                    Log.d("ND", "homeDesc" + homeDesc);
                    if (!mRecordMap.containsKey(homeDesc)) {
                       // sleepTimedDefault();
                        isJoinHomeDetail = tagTextClickShell(nodeInfo);
                        //  isJoinHomeDetail =  true;
                        NLogger.d(TAG, "点击了进入主页" + isJoinHomeDetail);
                        if (isJoinHomeDetail) {
                            isJoinHomeList = false;
                            mRecordMap.put(homeDesc, true);
                            currentAddCount++;
                            break;
                        }
                    }


                    if (q == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }
            }
        }
    }


    private void beginSelectFriend() {
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, LIST_VIEW);
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                   // ToastUtil.showToast(AppContext.getInstance().getContext(), "scrllorNodeInfo listView null");
                    return;
                }
                if(scrllorNodeInfo.getChildCount()==0){
                    stopTask();
                }
                for (int q = 0; q < scrllorNodeInfo.getChildCount(); q++) {

                    if (currentFriendsCount == maxFriendsCount) {
                        currentFriendsCount = 0;
                        //开始点击
                        tagTextClick(FBStringUtil.getString(StringConst.TEAM_YQXZHY));
                        isFrinedsListBack = true;
                        //停止任务
                        break;
                    }



                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(q);

                    //sleepTimedDefault();
                    currentFriendsCount++;
                    NLogger.d(TAG, "点击了确认" + NodeUtil.performClick(as));

                    if (q == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }

            }
        }
    }


    public void setParms(int maxAddCount,int minAddTime,int maxAddTime,int maxFriendsCount,String content){
        //stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.content = content;
        this.maxFriendsCount = maxFriendsCount;
        this.currentFriendsCount = 0;
        this.mRecordMap.clear();
        this.initFbMain = false;
        isClickSeatch = false;
        isSearchHomeList = false;
        isJoinHomeList = false;
        isJoinHomeDetail = false;
    }

}

